<?php

namespace Common\Model;

use Common\Model\XuecheModel;

class SendsmsModel extends XuecheModel {

    protected $tableName = 'sendsms';
    protected $fields = array(
        'mobile', 'code', 'create_time', 'status', '`use`'
    );

//    protected $_map = array(
//        'yue' => 'advance',
//        'f_member_id' => 'refer_id',
//    );

    /**
     * 清空数据缓存
     * 
     */
    public function ClearAllCache() {
        
    }

    //缓存设置
    protected $_useCacheType = 'file';
    protected $_catheTime = 36000;

    public function Validate($data) {
        if (empty($data['mobile'])) {
            return false;
        }
        return true;
    }

    /**
     * 检验短信发送时间
     */
    public function checkSendTime($mobile) {
        $info = $this->where("`mobile` = '{$mobile}'")->order('create_time DESC')->find();
        //var_dump($info);
        if ($info) {
            if (time() > ($info['create_time'] + 180)) {
                return TRUE;
            }
        }
        return true;
    }

    /**
     * 短信验证码核实
     */
    public function checkSmsCode($mobile, $code) {
        if (!isMobile($mobile)) {
            return false;
        }
        $time = time() - 300;
        $where = "`mobile`='{$mobile}' AND `code` = '{$code}' AND create_time >= '{$time}'";
        \Think\Log::write('SEND CODE:'.var_export($where,1));
        if ($res = $this->where($where)->find()) {
            return $res;
        }
        return FALSE;
    }

    /**
     * 短信使用操作
     */
    public function isUse($id) {
        if ($id > 0) {
            $data = array('use' => 'y');
            if ($this->where("`id` = '{$id}'")->save($data)) {
                return TRUE;
            }
        }
        return false;
    }
    
    public function GetDataByWhere($where = '', $order = '', $start = 0, $rows = 30, $fields = '', $hasCount = false, $bigData = FALSE) {
        return parent::GetDataByWhere($where, $order, $start, $rows, $fields, $hasCount, $bigData);
    }

}
